package com.tanhua.mangement.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.ObjectId;
import com.tanhua.common.pojo.UserInfo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.api.VideoApi;
import com.tanhua.dubbo.server.pojo.Video;

import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.mangement.vo.ManageVideosVo;
import com.tanhua.mangement.vo.PageResult;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ManageVideosService {

    @Autowired
    private UserInfoService userInfoService;


    @Reference(version = "1.0.0")
    private VideoApi videoApi;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;


    /**
     * 查询视频列表
     *
     * @param uid
     * @param page
     * @param pageSize
     * @param sortProp
     * @param sortOrder
     * @return
     */
    public PageResult queryManageVideos(Long uid, Integer page, Integer pageSize, String sortProp, String sortOrder) {


        //分页
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        PageInfo<Video> videoPageInfo = videoApi.queryVideoCountById(uid, page, pageSize, sortProp, sortOrder);

        //传入该用户所有的视频数
        pageResult.setCounts(videoPageInfo.getTotal());

        //取出当前也的数据
        List<Video> allVideoList = videoPageInfo.getRecords();

        //判断返回数据是否为空
        if (CollectionUtil.isEmpty(allVideoList)) {
            return pageResult;
        }

        //获取用用户信息
        UserInfo userInfo = userInfoService.queryUserInfoByUserId(uid);

        List<ManageVideosVo> ManageVideosVoList = new ArrayList<>();

        //遍历集合将数据封装到对象
        for (Video video : allVideoList) {
            ManageVideosVo videosVo = new ManageVideosVo();
            videosVo.setId(video.getId().toHexString());
            videosVo.setNickname(userInfo.getNickName());
            videosVo.setUserId(video.getUserId().intValue());

            videosVo.setCreateDate(video.getCreated());
            //举报数
            videosVo.setReportCount(RandomUtils.nextInt(0, 500));
            //喜欢数
            videosVo.setLikeCount(RandomUtils.nextInt(0, 500));
            //评论数
            videosVo.setCommentCount(RandomUtils.nextInt(0, 500));
            //转发数
            videosVo.setForwardingCount(RandomUtils.nextInt(0, 500));

            videosVo.setVideoUrl(video.getVideoUrl());
            videosVo.setPicUrl(video.getPicUrl());

            //将对象封装到集合中
            ManageVideosVoList.add(videosVo);

        }
        pageResult.setItems(ManageVideosVoList);

        return pageResult;
    }
}
